package com.riaevolution.components.breadcrumb {
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.ButtonBar;
	import mx.core.ClassFactory;
	import mx.core.IFlexDisplayObject;
	import mx.core.mx_internal;

	/**
	 * Bread crumb navigator link bar button.
	 * This show a nice breadcrumb navigation bar.
	 */
	public class BreadCrumbsLinkBar extends ButtonBar {

		public function BreadCrumbsLinkBar() {
			super();
			mx_internal::navItemFactory = new ClassFactory(BreadCrumbButton);
		}

		public var smalButtonWidth:Number = 16;

		private var _maxNums:int = -1;
		
		/**
		 * handler para hacer alguna mierda con el  
		 */
		public var buttonCreationCallBack:Function = undefined;

		public function get maxVisibleButtons():int {
			return _maxNums;
		}

		/**
		 * Set a maximun visible buttons for the nav bar.
		 * This allow you to resize the buttons after your max visible limit.
		 * After these limit al buttons wil be visible.
		 * Ejemplo:<br />
		 * Esto va a mostrar solo los 2 ultimos botones del breadcrumbs.
		 * <listing>
		 * breadcrumbs.maxVisibleButtons = Math.max(breadcrumbs.dataProvider.length - 2,0);
		 * </listing>
		 *
		 * @param value int
		 */
		public function set maxVisibleButtons(value:int):void {
			//if (_maxNums < value) {
			if (dataProvider) {
				dataProvider.regresh();
			}
			//}
			_maxNums = value;
			//invalidateDisplayList();
		}

		private var _limitFrom:String = "start";

		public function set limitFrom(value:String):void {
			if (value == "start" || value == "end") {
				_limitFrom = value;
			}

			if (dataProvider) {
				dataProvider.regresh();
			}
			//invalidateDisplayList();
		}

		public function get limitFrom():String {
			return _limitFrom;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			var t:int = 0;
			var c:Button;
			t = numChildren - 1;
			if(buttonCreationCallBack is Function) {
				while(t >= 0) {
					c = getChildAt(t) as Button;
					buttonCreationCallBack(c);
					t--;
				}
			}
			t = 0;

			if (_maxNums != -1 && !isover) {
				switch (limitFrom) {
					case 'start':
						if (numChildren > _maxNums) {
							while (t < _maxNums) {
								c = getChildAt(t) as Button;
								c.width = smalButtonWidth;
								t++;
							}
						}
						break;
					case 'end':
						t = numChildren - _maxNums;
						if (t > 0) {
							while (t > 0) {
								t--;
								c = getChildAt(t) as Button;
								c.width = smalButtonWidth;
							}
						}
						break;
				}
			}
		}

		override public function set dataProvider(value:Object):void {
			super.dataProvider = value;
			super.invalidateDisplayList();
		}

		private var isover:Boolean = false;

		private function handle_MO(e:MouseEvent):void {
			var idx:int = this.getChildIndex(e.currentTarget as DisplayObject);

			if (e.type == MouseEvent.ROLL_OVER) {
				isover = true;

				if (idx < _maxNums) {
					(e.currentTarget as Button).width = (e.currentTarget as Button).measuredMinWidth;
				}
			} else {
				isover = false;

				if (idx < _maxNums) {
					(e.currentTarget as Button).width = smalButtonWidth;
				}
			}
		}

		/**
		 * Override here and set the firstChild property of the button.
		 *
		 * @param label
		 * @param icon
		 * @return
		 *
		 */
		override protected function createNavItem(label:String, icon:Class = null):IFlexDisplayObject {
			var newButton:BreadCrumbButton = super.createNavItem(label, icon) as BreadCrumbButton;
			var n:int = this.getChildIndex(newButton);
			newButton.useHtml = useHtml;
			newButton.firstChild = (n == 0);
			if(buttonCreationCallBack is Function) {
				buttonCreationCallBack(newButton);
			}
			/* newButton.addEventListener(MouseEvent.ROLL_OUT, handle_MO, false, 0, true);
			 newButton.addEventListener(MouseEvent.ROLL_OVER, handle_MO, false, 0, true); */
			return newButton;
		}
		/**
		 * Usar texto html en los botones. 
		 */		
		public var useHtml:Boolean = false;
	}
}